﻿using System;
using System.Data;
using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;
using System.Web;
using System.Web.Caching;


using LMD.Constant;
using LMD.Lib.Oracle;

namespace Cached
{
    /// <summary>
    /// Summary description for ICPAdminTable
    /// </summary>
    public class ICPAdminTable : CacheAbstraction
    {
        public DataTable GetAdminList()
        {
            return GetAdminList(false);
        }
        public DataTable GetAdminList(bool bForceRefresh)
        {
            String sxCacheKey = KCache.ICP_ADMIN;

            if(bForceRefresh)
            {
                HttpRuntime.Cache.Remove(sxCacheKey);
            }

            if (HttpRuntime.Cache[sxCacheKey] == null)
            {
                DBLib dbl = new DBLib();

                String sxQuery =
                    @"
                    select
                        m1.ICP_ADMIN_ID ID,
                        m1.ICP_USER_ID ADMIN_NO,
                        v1.EMPLOYEE_NAME ADMIN_NAME,
                        m1.CREATE_BY CREATOR_NO,
                        v2.EMPLOYEE_NAME CREATOR_NAME,
                        m1.CREATE_DATE
                    from 
                        ICP_ADMIN m1
                        left join V_ICP_USER v1 on m1.ICP_USER_ID = v1.ICP_USER_ID
                        left join V_ICP_USER v2 on m1.CREATE_BY = v2.ICP_USER_ID
                    order by
                        m1.ICP_USER_ID ASC
                    ";

                DataTable dt = dbl.GetDataTable(sxQuery);

                AddToCache(sxCacheKey, dt, CacheType.Daily);
                
            }
            return (DataTable)HttpRuntime.Cache[sxCacheKey];
        }        

        public bool IsAdmin(String sxUserId)
        {
            return IsAdmin(sxUserId, false);
        }
        public bool IsAdmin(String sxUserId, bool bForceRefresh)
        {
            DataTable dt = GetAdminList(bForceRefresh);
            DataView dv = new DataView(
                dt, 
                String.Format("ADMIN_NO = '{0}'", sxUserId), 
                String.Empty, 
                DataViewRowState.CurrentRows);

            return dv.Count > 0;
        }

        public override String GetCacheFilename()
        {
            return KCache.Filename.ADMIN_LIST;
        }
    }
}